package com.itcast.yygh.hosp.controller;

import com.itcast.common.result.R;
import com.itcast.yygh.hosp.service.HospitalService;
import com.itcast.yygh.model.model.hosp.Hospital;
import com.itcast.yygh.model.vo.hosp.HospitalQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 医疗项目后台管理系统医院列表模块的显示
 */
@Api(tags = "医院管理模块")
@RestController
@RequestMapping("/admin/hosp/hospital")
//@CrossOrigin
public class HospitalController {
    private static final Logger log = LoggerFactory.getLogger(HospitalController.class);
    // 引入service服务
    @Resource
    private HospitalService hospitalService;

    // 医院数据分页查询
    @ApiOperation("医院数据分页查询")
    @PostMapping("/findPage/{page}/{limit}")
    public R findPage(@PathVariable Integer page,
                      @PathVariable Integer limit,
                      @RequestBody HospitalQueryVo queryVo) {
        // 调用service查询分页数据
        Page<Hospital> pages = hospitalService.findPage(page, limit, queryVo);
        // 返回对应结果集
        return R.ok().data("pages", pages);
    }

    //更新医院的上线状态
    @ApiOperation(value = "更新上线状态")
    @GetMapping("updateStatus/{id}/{status}")
    public R lock(
            @ApiParam(name = "id", value = "医院id", required = true)
            @PathVariable("id") String id,
            @ApiParam(name = "status", value = "状态（0：未上线 1：已上线）", required = true)
            @PathVariable("status") Integer status){
        try {
            hospitalService.updateStatus(id, status);
        }catch (Exception e) {
            log.debug("医院锁定状态变更失败: {}", e.getMessage(), e);
            return R.error();
        }
        return R.ok();
    }

    //查询医院详情接口
    @ApiOperation("获取医院详情")
    @GetMapping("show/{id}")
    public R show(
            @ApiParam(name = "id", value = "医院id", required = true)
            @PathVariable String id) {
        Map<String, Object> hospital = hospitalService.show(id);
        return R.ok().data("hospital",hospital);
    }
}
